layui.use(['form','layer','table','laytpl'],function(){
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        laytpl = layui.laytpl,
        table = layui.table;

    //用户列表
    var tableIns = table.render({
        elem: '#countList',
        url : 'http://127.0.0.1:88/index.php/count/count/countList?id='+localStorage.login_name,
        cellMinWidth : 95,
        page : true,
        height : "full-125",
        limits : [10,15,20,25],
        limit : 20,
        id : "countListTable",
        cols : [[
            {type: "checkbox", fixed:"left", width:50},
            {field: 'count_id', title: 'ID' ,width:50, align:"center"},
            {field: 'user_name', title: '用户名', align:"center"},
            {field: 'count_type', title: '支付平台', align:"center"},
            {field: 'count_num', title: '账户', align:'center'},
            {field: 'count_name', title: '昵称/所属银行',  align:'center'},
            {field: 'count_addTime', title: '添加时间', align:'center'},
            {title: '操作', templet:'#countListBar', align:"center"}
        ]]
    });

    //搜索【此功能需要后台配合，所以暂时没有动态效果演示】
    $(".search_btn").on("click",function(){
    	var type = $(this).data('type');
    	//console.log(type);
        if($(".searchVal").val() != ''){
            table.reload("countListTable",{
                page: {
                    curr: 1 //重新从第 1 页开始
                },
                method:'post',
                url:'http://127.0.0.1:88/index.php/count/count/countSearch',
                where: {
                    key: $(".searchVal").val(),  //搜索的关键字
                    user_name:localStorage.login_name
                },
                contentType: 'application/json',
                done: function(res, curr, count){
                  //this.where={};
                  //不清空，有问题：搜索条件会保留
                  //清空，  有问题：分页的时候没有搜索条件了,分页会受到影响
              }
            })
        }else{
            layer.msg("请输入搜索的内容");
        }
    });

    //添加用户
    function addcount(edit){
        var index = layui.layer.open({
            title : "编辑",
            type : 2,
            area:["550px","400px"],
            content : "countAdd.html",
            success : function(layero, index){
                /*var body = layui.layer.getChildFrame('body', index);
                body.find(".count_name").val(edit.count_name);  //登录名
                    body.find(".userEmail").val(edit.userEmail);  //邮箱
                    body.find(".userSex input[value="+edit.userSex+"]").prop("checked","checked");  //性别
                    body.find(".userGrade").val(edit.userGrade);  //会员等级
                    body.find(".userStatus").val(edit.userStatus);    //用户状态
                    body.find(".userDesc").text(edit.userDesc);    //用户简介
                    form.render();*/
                /*if(edit){
                    body.find(".userName").val(edit.userName);  //登录名
                    body.find(".userEmail").val(edit.userEmail);  //邮箱
                    body.find(".userSex input[value="+edit.userSex+"]").prop("checked","checked");  //性别
                    body.find(".userGrade").val(edit.userGrade);  //会员等级
                    body.find(".userStatus").val(edit.userStatus);    //用户状态
                    body.find(".userDesc").text(edit.userDesc);    //用户简介
                    form.render();
                }*/
                setTimeout(function(){
                    layui.layer.tips('点击此处返回用户列表', '.layui-layer-setwin .layui-layer-close', {
                        tips: 3
                    });
                },500)
            }
        })
        //layui.layer.full(index);
        //改变窗口大小时，重置弹窗的宽高，防止超出可视区域（如F12调出debug的操作）
        $(window).on("resize",function(){
            layui.layer.full(index);
        })
    }
    $(".addNews_btn").click(function(){
        addcount();
    })
	//刷新count_refresh
	$(".count_refresh").click(function(){
        tableIns.reload();
    })
	
    //批量删除
    $(".delAll_btn").click(function(){
        var checkStatus = table.checkStatus('countListTable'),
            data = checkStatus.data,
            count_id = [];
            
        if(data.length > 0) {
            for (var i in data) {
                count_id.push(data[i].count_id);
            }
            layer.confirm('确定删除选中的用户？', {icon: 3, title: '提示信息'}, function (index) {
                $.get("http://127.0.0.1:88/index.php/count/count/delMorecount",{
                    count_id : count_id  //将需要删除的count_name作为参数传入
                },function(data){
                tableIns.reload();
                layer.close(index);
                })
            })
        }else{
            layer.msg("请选择需要删除的用户");
        }
    })

    //列表操作
    table.on('tool(countList)', function(obj){
        var layEvent = obj.event,
            data = obj.data;

        if(layEvent === 'edit'){ //编辑
        	//console.log(data.count_id);
            var index = layui.layer.open({
            title : "编辑商铺",
            type : 2,
            area:["450px","400px"],
            content : "countEdit.html",
            success : function(layero, index){
            	//console.log(data);
                var body = layui.layer.getChildFrame('body', index);
                body.find(".count_good_titles").val(data.count_good_titles);
                body.find(".count_good_img").attr("src",data.count_img_url);
                body.find(".count_good_price").val(data.count_good_price);
                body.find(".count_good_payPrice").val(data.count_good_payPrice);
                body.find(".count_good_money").val(data.count_good_money);
                body.find(".count_good_searchWord").val(data.count_good_searchWord);
                body.find(".count_good_shop").val(data.count_good_shop);
                body.find(".count_good_url").val(data.count_url);
                body.find(".count_url").val(data.count_url);
                body.find(".count_img_url").val(data.count_img_url);
                body.find(".count_shop_type").val(data.count_shop_type);
               	body.find(".count_yaoqiu").text(data.count_yaoqiu);
               	body.find("#count_id").val(data.count_id);
               	form.render();
                /*if(edit){
                    body.find(".userName").val(edit.userName);  //登录名
                    body.find(".userEmail").val(edit.userEmail);  //邮箱
                    body.find(".userSex input[value="+edit.userSex+"]").prop("checked","checked");  //性别
                    body.find(".userGrade").val(edit.userGrade);  //会员等级
                    body.find(".userStatus").val(edit.userStatus);    //用户状态
                    body.find(".userDesc").text(edit.userDesc);    //用户简介
                    form.render();
                }*/
                setTimeout(function(){
                    layui.layer.tips('点击此处返回用户列表', '.layui-layer-setwin .layui-layer-close', {
                        tips: 3
                    });
                },500)
            }
        })
        }else if(layEvent === 'usable'){ //启用禁用
            var _this = $(this),
                usableText = "是否确定禁用此用户？",
                btnText = "已禁用";
            if(_this.text()=="已禁用"){
                usableText = "是否确定启用此用户？",
                btnText = "已启用";
            }
            layer.confirm(usableText,{
                icon: 3,
                title:'系统提示',
                cancel : function(index){
                    layer.close(index);
                }
            },function(index){
                _this.text(btnText);
                layer.close(index);
            },function(index){
                layer.close(index);
            });
        }else if(layEvent === 'del'){ //删除
            layer.confirm('确定删除此用户？',{icon:3, title:'提示信息'},function(index){
                $.get("http://127.0.0.1:88/index.php/count/count/delCount",{
                    id : data.count_id  //将需要删除的newsId作为参数传入
                },function(data){
                	if(data.code==200){
                		layer.msg(data.msg);
                		setTimeout(function(){
                			tableIns.reload();
                    		layer.close(index);
                		},1000);
                	}else{
                		layer.msg(data.msg);
                	}
                })
            });
        }else if(layEvent === 'detail'){
        	var index = layui.layer.open({
            title : "店铺详情",
            type : 2,
            area:["450px","420px"],
            content : "countDetail.html",
            success : function(layero, index){
            	var body = layui.layer.getChildFrame('body', index);
            	$.get("http://127.0.0.1:88/index.php/count/count/detailcount",{
                    id : data.count_name  //将需要删除的newsId作为参数传入
                },function(data){
                	
                	body.find(".count_name").val(data.detail[0].count_name);
                    body.find(".count_wangwang").val(data.detail[0].count_name);
                    body.find(".count_type").val(data.detail[0].count_name);
                    body.find(".count_img").attr("src",data.detail[0].count_img_url);
                    form.render();
                })
                /*var body = layui.layer.getChildFrame('body', index);
                
                    body.find(".count_name").val("detail.userName");  //登录名
                    body.find(".userEmail").val(edit.userEmail);  //邮箱
                    body.find(".userSex input[value="+edit.userSex+"]").prop("checked","checked");  //性别
                    body.find(".userGrade").val(edit.userGrade);  //会员等级
                    body.find(".userStatus").val(edit.userStatus);    //用户状态
                    body.find(".userDesc").text(edit.userDesc);    //用户简介
                    form.render();*/
                
                setTimeout(function(){
                    layui.layer.tips('点击此处返回用户列表', '.layui-layer-setwin .layui-layer-close', {
                        tips: 3
                    });
                },500)
            }
        })
        }
    });

})